<template>
	<view style="position: relative;">
		<view class="fr-ac-jsa" :style="{height:headerHeight+'px'}">
			<view v-for="(item,ind) in list" :key="ind" class="fr-ac-jc flex-1" @click="dropdown(item,ind)" :class="activeIndex==ind?'dropdownActive':''">
				{{item.title}}
				<view :class="activeIndex==ind?'dropdownIcon':''">
					<up-icon :name="icon" customPrefix="custom-icon" size="20rpx" :color="activeIndex==ind?'#1676FF':''" class="dropdownIcon"></up-icon>
				</view>
			</view>
		</view>
		<view v-if="isTrue" class="dropdownMain animate__animated animate__fadeIn" :style="{top:addUnit(headerHeight)}">
			<scroll-view scroll-y="true">
				<view class="px-30 py-20 fw" >
					<view v-if="oldList.allName" class="dropdown" :class="newList.findIndex(e=>e.isTrue==true)==-1?'active':''" @click="toAllName">
						<view>{{oldList.allName}}</view>
					</view>
					<view v-for="(child,index) in newList" :key="index" class="dropdown" :class="child.isTrue?'active':''" @click="toCheckBox(child)">
						<view v-if="oldList.labelName" class="mr-10">{{child[oldList.labelName]}}</view>
						<view>{{child[oldList.keyName]}}</view>
					</view>
				</view>
			</scroll-view>
			
			<view class="fr-ac-jsa" v-if="oldList.checkbox" style="height: 100rpx;">
				<view class="dropdownBuuton" @click="reset(oldList)">重置</view>
				<view class="dropdownBuuton" @click="determine(oldList)">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { watch } from 'vue'
	import {addUnit } from '@/uni_modules/uview-plus/libs/function/index'
	export default {
		data() {
			return {
				value:[],
				activeIndex:null,
				isTrue:false,
				top:0,
				newList:[],
				oldList:[],
				selectedItem:null,
				isValue:false
			}
		},
		props: {
			headerHeight:{
				default: 40,
				type:[Number, String]
			},
			height:{
				default: 600,
				type:[Number, String]
			},
			list:{
				default: [],
				type:Array
			},
			icon:{
				default: "xiangxia",
				type:String
			}
		},
		computed: {},
		created() {},
		mounted() {},
		methods: {
			addUnit,
			dropdown(val,ind){
				if (this.activeIndex === ind) {
					if(this.isValue){
						 this.method(val)
					}else{
						this.isTrue=false
						this.activeIndex = null;
					}
				} else {
				    // 否则，显示下拉菜单并更新选中项
				    this.activeIndex = ind;
				    this.isTrue = true;
				}
				this.newList=val.list
				this.oldList=val
			},
			toCheckBox(newVal){
				this.isValue=true
				if(this.oldList.checkbox){
					newVal.isTrue=!newVal.isTrue
				}else{
					this.newList.forEach(item=>{
						item.isTrue=false
					})
					newVal.isTrue=true
					this.method(this.oldList)
				}
				this.$emit('select',newVal)
			},
			reset(val){
				val.list.forEach(item=>{
					item.isTrue=false
				})
			},
			determine(val){
				this.method(val)
			},
			method(val){
				let arr=val.list.filter(item=>item.isTrue==true)
				this.value= arr.map(item=>item[val.valueKeyName])
				if(!val.checkbox){
					this.$emit('radio',this.value,val.title)
				}else{
					this.$emit('checkBox',this.value,val.title)
				}
				this.isTrue=false
				this.activeIndex = null;
				this.isValue=false
			},
			toAllName(){
				this.oldList.list.forEach(item=>{
					item.isTrue=false
				})
				this.isValue=true
				if(!this.oldList.checkbox){
					this.isTrue=false
					this.activeIndex = null;
					this.isValue=false
					this.$emit('radio',[],this.oldList.title)
				}
			}
		}
	}
</script>

<style lang="scss">
	.dropdownMain{
		position: absolute;
		top:0;
		width:100%;
		background-color: #fff;
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* 下边框阴影 */
		z-index:996;
	}
	.dropdown{
		min-width: 21%;
		margin: 10rpx 1.9%;
		box-sizing: border-box;
		padding: 15rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #ccc;
		border-radius: 8rpx;
	}
	.active{
		transition: all 0.5s;
		border: 1px solid var(--color);
		background: var(--color);
		color: #fff;
	}
	.dropdownBuuton{
		padding: 15rpx 50rpx;
		border-radius: 12rpx;
		background:#999999;
		color: #fff;
		&:nth-child(2){
			background:var(--color);
		}
	}
	.dropdownActive{
		color:var(--color);
	}
	.dropdownIcon{
		transform: rotate(180deg);
	}
	.animate__animated{
		-webkit-animation-duration: 0.5s;
		animation-duration: 0.5s;
	}
</style>